//http://192.168.5.117:8010/publicProject  
//https://ty.tinglifangs.com/publicProject  正式环境
//https://ty-test.tinglifangs.com/publicProject 测试
const apiHttp = "https://api.docvitcd.com/mini"

function request(url: string, method: any, data: any, header: any = {}): Promise<any> {
  data = data || {};
  header['U-FS'] = 'ty'
  let userInfo = wx.getStorageSync("userInfo");
  if (userInfo.clientKey) {
    header.clientKey = userInfo.clientKey
  }
  wx.showNavigationBarLoading();
  let promise = new Promise(function (resolve, reject) {
    // wx.showLoading({ title: '加载中', icon: 'loading', duration: 10000 });
    (wx as any).request({
      url: apiHttp + url,
      header: header,
      data: data,
      method: method,
      success: function (res) {
        if (typeof res.data === "object") {
          if (res.data.code == 400) {
            wx.removeStorage({
              key: 'userInfo',
            })
            wx.redirectTo({
              url: '/pages/login/login',
            })
          } else if (res.data.code !== 200) {
            wx.showToast({
              title: res.data.msg || res.data.error,
              icon: "none"
            })

            reject(res.data);
          }
        }
        resolve(res.data);
      },
      complete: function () {
        // wx.hideLoading()
      }
    });

  });
  return promise.catch((res) => { return res });
}
module.exports = {
  "get": function (url: string, data = {}, header = {}) {
    return request(url, "GET", data, header);
  },
  "post": function (url: string, data = {}, header = {}) {
    return request(url, "POST", data, header);
  },
}